How to set a base member generic List using derived specific List

Change List inEdges to List inEdges and it will work. The problem is that Segments is known as a List Look up variance/covariance/contravariance if you're interested. ).

Up vote 0 down vote favorite share g+ share fb share tw.

I have a compiler error when trying to set a generic base collection class member in this derived class. Error CS0029: Cannot implicitly convert type 'System.Collections.Generic. List' to 'System.Collections.Generic.

List' Here's the outline of my generic collection public class Path : IEnumerable where T : Segment { private List segments = new List(); public List Segments { set { segments = value; } get { return this. Segments; } } public Path() { this. Segments = new List(); } public Path(List s) { this.

Segments = s; } } A derived generic class of this collection is then defined for a derived class IntSegment of Segment (for which the base collection is defined) public class IntersectionClosedPath : Path, IEnumerable where T : IntSegment { public IntersectionClosedPath(List inEdges) : base() { Segments = inEdges; } } I can't understand why this assignment is not allowed. (I don't need to make a deep copy of the incoming List). C# generics generic-list link|improve this question edited Mar 16 at 17:17phoog11k921 asked Mar 16 at 15:32gwizardry835 85% accept rate.

Change List inEdges to List inEdges and it will work. The problem is that Segments is known as a List, where T : IntSegment, while inEdges is a List. (For reasons I'll not go into here unless you ask, such an assignment is not allowed.

Look up variance/covariance/contravariance if you're interested. ).

1 that's a good alternative solution – Andras Zoltan Mar 16 at 15:43 Thanks this works...i'll bear the reading material in mind for later. I guess the only risk is that I have a collection which is supposed to contain the derived members but doesn't - if I accidently send in a List of the base. I'm not worried about this now, so no problem.

– gwizardry Mar 16 at 15:50 Very new to generics....but enjoying the possibilities achieveable with them! – gwizardry Mar 16 at 15:52 1 With the generics restrictions that exist, you won't be able to compile something that accidentally sends in a List of the base. What you send in will always have to be a List, and T will always have to match exactly.

E.g. This (assume class ExtendedIntSegment : IntSegment exists) won't even compile: new IntersectionClosedPath(new List()); Because ExtendedIntSegment and IntSegment aren't exactly the same. If you make those two types the same, either of those, it'll compile and work just fine, with great type safety.

– Tim S. Mar 16 at 16:01.

A classic problem. A List cannot be implicitly converted to a List. You can cast the items in a List and create a List like this: listOfBase = listOfDerived.Cast().ToList().

1 I'm being a pedant, but this won't quite work - Cast() returns an IEnumerable, not List – Andras Zoltan Mar 16 at 15:40 Grr! Forgot the ToList. Fixed now.

– Kendall Frey Mar 16 at 15:42 I don't quite understand this as i'm not trying to convert from base to derived but the other way round. – gwizardry Mar 16 at 15:49 D'oh! I must be still sleeping.

– Kendall Frey Mar 16 at 15:50.

A List is not equivalent to a List where T : TBase. Why? Because List doesn't inherit from List, only the generic type parameters are related.

You can instead do this in the constructor: Segments = inEdges.Cast().ToList() I would also change the constructor parameter to IEnumerable Equally, it might be that @Tim S. Has the best solution based on what you want to achieve. Personally I believe he's probably nailed it.

– gwizardry Mar 16 at 16:04 @gwizardry - In IntersectionClosedPath yes. – Andras Zoltan Mar 16 at 16:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.